package com.lw.extraceim.ui.activity;

import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RadioGroup;


import com.lw.extraceim.R;
import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.service.MapService;
import com.lw.extraceim.ui.activity.base.BaseActivity;
import com.lw.extraceim.ui.fragments.base.BaseFragment;
import com.lw.extraceim.ui.fragments.CollectFragment;
import com.lw.extraceim.ui.fragments.MeFragment;
import com.lw.extraceim.ui.fragments.PackageFragment;
import com.lw.extraceim.ui.fragments.TaskFragment;
import com.lw.extraceim.zxing.ExtraceCaptureActivity;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    private RadioGroup radioGroup;
    private FrameLayout frameLayout;
    private ArrayList<BaseFragment> fragments;
    private int position = 0;//显示的页面
    private BaseFragment nowFragment;//当前显示的fragment
    private SearchView mSearchView;
    private ImageButton mScanButton;

    private Toolbar mToolbar;

    private CollectFragment collectFragment;
    private PackageFragment packageFragment;
    private TaskFragment taskFragment;
    private MeFragment meFragment;

    private MapService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this,MapService.class);
        Log.d(TAG, "onCreate: 启动MapService");
        startService(intent);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        mToolbar = findViewById(R.id.toolbar);
        mSearchView = findViewById(R.id.search_view);
        mScanButton = findViewById(R.id.scan_button);
        mScanButton.setVisibility(View.GONE);
        initToolBar();
//        actionBar.hide(); //隐藏actionBar
        radioGroup = findViewById(R.id.rg_main);
        frameLayout = findViewById(R.id.frameLayout_main);

    }

    /**
     * 初始化toolBar
     */
    protected void initToolBar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mSearchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivity.hideSoftKeyboard(MainActivity.this);
                SearchActivity.actionStart(MainActivity.this, AppConstants.SEARCH_ES, null);
            }
        });
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startQRScan(MainActivity.this,ExtraceCaptureActivity.class,"扫描条形码",ExtraceCaptureActivity.SCAN_SEARCH);
            }
        });

    }
    @Override
    public void initData() {
        //初始化各个fragment
        fragments = new ArrayList<>();
        collectFragment = new CollectFragment(this,application);
        packageFragment = new PackageFragment(this,application);
        taskFragment = new TaskFragment(this,application);
        meFragment = new MeFragment(this,application);
        fragments.add(collectFragment);
        fragments.add(packageFragment);
        fragments.add(taskFragment);
        fragments.add(meFragment);
    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void initEvent() {
        //为radioGroup设置监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        position = 0;
                        break;
                    case R.id.rb_package:
                        position = 1;
                        break;
                    case R.id.rb_transport:
                        position = 2;
                        break;
                    case R.id.rb_user:
                        position = 3;
                        break;
                }
                BaseFragment nextFragment = getFragment(position);
                switchFragment(nowFragment, nextFragment);
            }
        });
        radioGroup.check(R.id.rb_home);

    }

    //切换fragment
    private void switchFragment(BaseFragment fromFragment, BaseFragment nextFragment) {
        if (fromFragment != nextFragment) {
            nowFragment = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断 nextFragment 是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.frameLayout_main, nextFragment).commit();
                } else {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    //根据位置获得相应的fragment
    private BaseFragment getFragment(int position) {
        return fragments.get(position);
    }

    public static void actionStart(Context context){
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }
}
